package honest.playmusic.util;

import honest.playmusic.bean.Music;
import honest.playmusic.data.ConfigData;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.TreeMap;

/**
 * 本地歌词管理
 * @author 李跃东
 * @mail androiddevelop@qq.com
 * @date 2013-11-8
 */

public class LocalLrcManager{

	/**
	 * 获取歌词队列
	 * @param music 音乐
	 * @return
	 */
	public TreeMap<Integer,String> getLrc(Music music) {
		String lrcPath= music.getMusicAbsolutePath();
		lrcPath=lrcPath.substring(0, lrcPath.length()-3)+"lrc";
		File file=new File(lrcPath);
		OnlineLrcManager onlineLrcManager=new OnlineLrcManager();

		//歌词文件不存在,在线获取
		if(!file.exists()){
			if(!onlineLrcManager.getLrcAndWriteFile(music))
				return new TreeMap<Integer,String>();
		}

		return	getLrcQueue(file);
	}


	/**
	 * 得到歌词集合队列
	 * 歌词文件文件不存在，则返回空队列
	 * @param lrcFile 歌词文件
	 * @return
	 */
	private TreeMap<Integer, String> getLrcQueue(File lrcFile){
		TreeMap<Integer, String> treeMap=new TreeMap<Integer,String>();
		SimpleDateFormat sdf=new SimpleDateFormat("[mm:ss.SS");
		if(!lrcFile.exists())
			return treeMap;

		try {
			BufferedReader br=new BufferedReader(new FileReader(lrcFile));
			String tmp;
			while((tmp=br.readLine())!=null){
				//除去无用字符及
				tmp=tmp.trim();
				//空行跳过
				if(tmp.length()==0)
					continue;

				List<String> list=FormatString.lrcStringSplit(tmp);
				long time;
				for(int t=0;t<list.size();t++){
					tmp=list.get(t);
					if(tmp.replaceAll("\\[\\d\\d:\\d\\d\\.\\d\\d\\]", "").length()==0)
						tmp=tmp+" ";
					//进行划分
					String[] str=tmp.split("]");
					try {
						final long baseTime=sdf.parse("[00:00.00").getTime();
						switch(str.length){
						//歌词开始部分
						case 1:{
							if(str[0].matches("\\[\\d\\d:\\d\\d\\.\\d\\d")){
								time=sdf.parse(str[0].substring(0, 9)).getTime()-baseTime-ConfigData.TIME;
								treeMap.put((int)time,"");
							}else{
								time=0-ConfigData.TIME;
								while((treeMap.containsKey((int)time)))
									time++;
								treeMap.put((int)time,englishToChinese(str[0].substring(1)));
							}
							break;
						}

						//正常歌词
						case 2:{
							if(str[0].matches("\\[\\d\\d:\\d\\d\\.\\d\\d")){
								time=sdf.parse(str[0].substring(0, 9)).getTime()-baseTime-ConfigData.TIME;
								treeMap.put((int)time, str[1].trim());
							}else{
								//非标准格式，忽略	
							}
							break;
						}

						//缩写歌词
						default:{
							for(int i=0;i<str.length-1;i++){
								if(str[i].matches("\\[\\d\\d:\\d\\d\\.\\d\\d")){
									time=sdf.parse(str[i].substring(0, 9)).getTime()-baseTime-ConfigData.TIME;
									treeMap.put((int)time, str[str.length-1].trim());
								}
							}
						}
						}
					} catch (ParseException e) {
						e.printStackTrace();
					}
				}
			}
			br.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return treeMap;
	}


	/**
	 * 歌词开始部分替换
	 * @return
	 */
	private String englishToChinese(String  str){
		str=str.replace("ti:","");
		str=str.replace("ar:","歌手: ");
		str=str.replace("al:","专辑: ");
		str=str.replace("by:","歌词: ");
		return str;
	}

}
